<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class BannerController extends Controller{
	public function __construct()
    {
        $this->middleware('admin');
    }
    public function banner()
    {
        return view('admin.banner.banner');
    }
    public function bannerdata(Request $request){
        $pageNum=$request->input('page');
        $limit=$request->input('limit');
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        $banners = DB::table('banner')->where('state','<',3)->orderBy('id','desc')->offset($page)->take($limit)->get();
        $count = DB::table('banner')->where('state','<',3)->orderBy('id','desc')->count();
        foreach ($banners as $k=>$item){
            $banners[$k]->time =date('Y-m-d H:i:s',$banners[$k]->time);
            $banners[$k]->uptime =date('Y-m-d H:i:s',$banners[$k]->uptime);
            // $banners[$k]->state = $item->state==1 ? '启用' : '禁用';
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $count,
            'data'  => $banners
        ];
        return response()->json($data);
    }
    public function addbanner(Request $request)
    {
        if ($request->isMethod('post')) {
            DB::table('banner')->insert(['title' => $request->title,'img' => $request->img, 'state' =>1, 'pro_id' =>$request->pro_id,'sort' =>$request->sort, 'time' =>time(), 'uptime' =>time()]);
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
        return view('admin.banner.addbanner');
    }
    public function editbanner(Request $request)
    {
        $res=DB::table('banner')->where('id',$request->id)->first();
        $id=$request->id;
        $title=$res->title;
        $img=$res->img;
        $sort=$res->sort;
        $pro_id=$res->pro_id;
        return view('admin.banner.editbanner', compact('id','title','img','sort','pro_id'));
    }
    public function editbannerup(Request $request)
    {
        if ($request->isMethod('post')) {
            DB::table('banner')->where('id',$request->id)->update(['title' => $request->title,'img' => $request->img, 'pro_id' =>$request->pro_id, 'sort' =>$request->sort, 'uptime' =>time()]);
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
    }
    public function delbanner(Request $request)
    {
            $res=DB::table('banner')->where('id',$request->id)->update(['state'=>3]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function state_off(Request $request)
    {
            $res=DB::table('banner')->where('id',$request->id)->update(['state'=>2]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function state_on(Request $request)
    {
            $res=DB::table('banner')->where('id',$request->id)->update(['state'=>1]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
}